package com.wbao.dianniu.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.FriendListAdapter;
import com.wbao.dianniu.data.HeadData;
import com.wbao.dianniu.listener.IFriendListListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.FriendListManager;

import java.util.List;

/**
 * Created by Administrator on 2017/8/21.
 */

public class SelectFriendActivity extends BaseActivity implements IFriendListListener, FriendListAdapter.OnItemClickListener {
    private LinearLayout waittingLayout;
    private ProgressBar waittingProBar;
    private TextView waittingTV;
    private FriendListManager manager;
    private TextView noDataTV;
    private PullToRefreshListView pullListView;

    private FriendListAdapter adapter;
    private int ROWS = 10;
    private ListView myListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_follow_list, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        setTitleName(getResources().getString(R.string.select_friend));
        initWattingView();
        initRecycler();
        initManager();
        reqPullUpData();
    }

    private void initWattingView() {
        waittingLayout = findViewById(R.id.waitting_layout);
        waittingProBar = findViewById(R.id.waitting_progress);
        waittingTV = findViewById(R.id.waitting_text_desc);
        noDataTV = findViewById(R.id.nodata_text);
    }

    private void initRecycler() {
        noDataTV = findViewById(R.id.no_find_data);
        pullListView = findViewById(R.id.follow_list_recycler);
        pullListView.setMode(PullToRefreshListView.Mode.PULL_UP_TO_REFRESH);
        pullListView.setOnRefreshListener(onRefreshListener);
        myListView = pullListView.getRefreshableView();
        registerForContextMenu(myListView);
        adapter = new FriendListAdapter(this);
        adapter.setItemClickListener(this);
        myListView.setAdapter(adapter);
    }

    PullToRefreshBase.OnRefreshListener onRefreshListener = new PullToRefreshBase.OnRefreshListener() {
        @Override
        public void onRefresh() {
            reqPullUpData();
        }
    };

    private void initManager() {
        manager = new FriendListManager(this);
        manager.addFriendListener(this);
    }


    private void reqPullUpData() {
        manager.friendList(GlobalContext.getAccountId(), adapter.getList().size(), ROWS);
    }

    @Override
    public void onFriendFailure(int errorCode, String errorMsg) {
        waittingLayout.setVisibility(View.VISIBLE);
        waittingProBar.setVisibility(View.GONE);
        waittingTV.setText(errorMsg == null ? "" : errorMsg);
        pullListView.onRefreshComplete(false, false);
    }

    @Override
    public void onFriendSuccess(List<HeadData> objdata) {
        waittingLayout.setVisibility(View.GONE);
        pullListView.onRefreshComplete(false, false);
        adapter.addData(objdata);
        if (adapter.getList().size() == 0) {
            noDataTV.setVisibility(View.VISIBLE);
        } else {
            noDataTV.setVisibility(View.GONE);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeFriendListener(this);
        }
    }

    @Override
    public void itemClick(View v) {
        int position;
        switch (v.getId()) {
            case R.id.friend_list_layout:
                position = (Integer) v.getTag(R.id.friend_list_layout);
                //这里要利用adapter.getItem(position)来获取当前position所对应的对象
                String userName = adapter.getList().get(position).realName;
                int friendId = adapter.getList().get(position).friendId;
                Intent intent = getIntent();
                // 把返回数据存入Intent
                intent.putExtra(Const.INTENT_NICENAME, userName);
                intent.putExtra(Const.BUNDLE_ACCOUNTID, friendId);
                // 设置返回数据
                setResult(RESULT_OK, intent);
                SelectFriendActivity.this.finish();
                break;

            default:
                break;
        }
    }


}
